package com.mutouren.common.web;

import com.mutouren.common.entity.PageInfo;

/**
 * BootStrap风格分页类
 */
public class BootStrapPage {
	
	private final static int maxButtonCount = 5;
	private final static String methodFormat = "javascript:onPageClick(%d)";
	private final static String nomethod = "#";
	private final static String buttonFormat = "<li><a href=\"%s\">%s</a></li>\n";
	private final static String classButtonFormat = "<li class=\"%s\"><a href=\"%s\">%s</a></li>\n";		
		
	public static HtmlPage buildHtmlPage(PageInfo page) {
		return buildHtmlPage(page.getTotal(), page.getPageSize(), page.getPageIndex());
	}
	
	/**
	 * 构建分页信息
	 * note:
	 * a. pageIndexc从1开始计数
	 */
	public static HtmlPage buildHtmlPage(int total, int pageSize, int curIndex) {
		HtmlPage result = new HtmlPage();
		
		if ((total < 0) || (pageSize < 1) || (curIndex < 1)) {
			throw new RuntimeException("参数不合乎逻辑");
		}
		curIndex = autoAdjustPageIndex(total, pageSize, curIndex);
		result.setPageIndex(curIndex);
		
		result.setDescription(buildDescription(total, pageSize, curIndex));
		result.setHtml(buildHtml(total, pageSize, curIndex));
		return result;
	}
	
	private static int autoAdjustPageIndex(int total, int pageSize, int pageIndex) {		
		int maxIndex = calculateMaxIndex(total, pageSize);
		return pageIndex > maxIndex? maxIndex : pageIndex;
	}
	
	private static int calculateMaxIndex(int total, int pageSize) {
		if (total == 0) return 1;		
		return (int)Math.ceil((double)total / pageSize);
	}
	
	private static String buildDescription(int total, int pageSize, int pageIndex) {
		String format0 = "当前显示0条,共0条";
		String format1 = "当前显示%d~%d条,共%d条";
		
		if (total == 0) {
			return format0;
		} else {
			int begin = pageSize * (pageIndex - 1);
			return String.format(format1,
					begin + 1,
					pageSize * pageIndex > total ? begin + (total % pageSize) : begin + pageSize,
					total);
		}
	}
	
	private static String buildHtml(int total, int pageSize, int curIndex) {
		int maxIndex = calculateMaxIndex(total, pageSize);
		StringBuilder sbResult = new StringBuilder();
		
		// 前页
		sbResult.append(createPreviousButton(curIndex));
		
		// 页码
		int firstIndex = 1;
		if (maxIndex > maxButtonCount) {
			firstIndex = curIndex < maxButtonCount ? 1 : curIndex - maxButtonCount + 2;
			if (curIndex == maxIndex) {
				firstIndex--;
			}
		}
						
		for(int i = firstIndex; i < firstIndex + maxButtonCount; i++) {
			sbResult.append(createIndexButton(i, i == curIndex));			
			if (i == maxIndex) break;
		}
		
		// 省略
		if (firstIndex + maxButtonCount - 1 < maxIndex) {
			sbResult.append(createEllipsisButton());
		}
		
		// 后页
		sbResult.append(createNextButton(curIndex, maxIndex));		
		return sbResult.toString();
	}
	
	private static String createPreviousButton(int index) {
		if (index == 1) {
			return String.format(classButtonFormat, "disabled", nomethod, "&lt;");
		} else {
			String method = String.format(methodFormat, index - 1);
			return String.format(buttonFormat, method, "&lt;");
		}
	}
	
	private static String createNextButton(int index, int maxIndex) {
		if (index == maxIndex) {
			return String.format(classButtonFormat, "disabled", nomethod, "&gt;");
		} else {
			String method = String.format(methodFormat, index + 1);
			return String.format(buttonFormat, method, "&gt;");
		}
	}
	
	private static String createEllipsisButton() {
		return String.format(classButtonFormat, "disabled", nomethod, "..");
	}
	
	private static String createIndexButton(int index, boolean isActive) {
		if (isActive) {
			return String.format(classButtonFormat, "active", nomethod, index);
		} else {
			String method = String.format(methodFormat, index);
			return String.format(buttonFormat, method, index);
		}
	}	

	public static class HtmlPage {
		private String description;
		private String html;
		private int pageIndex;
		
		public String getDescription() {
			return description;
		}
		public String getHtml() {
			return html;
		}
		public void setDescription(String description) {
			this.description = description;
		}
		public void setHtml(String html) {
			this.html = html;
		}
		public int getPageIndex() {
			return pageIndex;
		}
		public void setPageIndex(int pageIndex) {
			this.pageIndex = pageIndex;
		}
	}
	
	public static void main(String[] args) {		
		HtmlPage page = buildHtmlPage(119, 10, 1);
		
		System.out.println("..............." + page.description);
		System.out.println("...............\n" + page.html);
	}	
}
